package com.seer.seertest.controller;

import com.seer.seertest.entity.Result;
import com.seer.seertest.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Slf4j
public class LoginController {

    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";

    @PostMapping("/sign-in")
    public Result signIn(@RequestBody Map<String, String> loginData) {
        log.info("登录详情：{}", loginData);
        // 参数校验
        if (loginData == null || loginData.isEmpty()) {
            return Result.error("请求体不能为空");
        }

        String username = loginData.get("username");
        String password = loginData.get("password");

        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return Result.error("用户名或密码不能为空");
        }

        // 凭证验证
        if (!USERNAME.equals(username) || !PASSWORD.equals(password)) {
            return Result.error("用户名或密码错误");
        }else {
            // 生成JWT Token
            String token = JwtUtils.generateToken(username);

            Map<String, Object> response = new HashMap<>();
            response.put("token", token);
            response.put("expiresIn", 3600);
            return Result.success(response);
        }
    }

    @PostMapping("/inbound/create")
    public Result createOrder(@RequestBody Map<String, Object> order) {
        log.info("==== 接收到订单创建请求 ==== {}", order);
        return Result.success();
    }


}
